import json
import logging

from django.shortcuts import render, redirect, HttpResponse
from django.db.models import Q

from mac import models
from mac.forms import jurisdiction_forms
from mac.utils import page
from mac.utils import encrypt

logger = logging.getLogger(__name__)


def jurisdiction_list(request):
    '''
    展示权限
    :param request:
    :return:
    '''
    if request.method == 'POST':
        search = request.POST.get('search')
        jurisdiction_query = models.Jurisdiction.objects.filter(Q(jurisdiction__icontains=search)
                                                            | Q(url__icontains=search))
    else:
        jurisdiction_query = models.Jurisdiction.objects.all()
    jurisdiction_count = jurisdiction_query.count()
    pages = request.GET.get('page')
    jurisdiction_page_size = request.GET.get('page_size')
    if not jurisdiction_page_size:
        jurisdiction_page_size = request.session.get('jurisdiction_page_size', 20)
    else:
        request.session['jurisdiction_page_size'] = request.GET.get('page_size')
    jurisdiction_obj = page.PageInfo(current_page=pages,
                                    all_count=jurisdiction_count,
                                    url='/jurisdiction_list/',
                                    per_page=jurisdiction_page_size)
    jurisdiction_page_obj = jurisdiction_query[jurisdiction_obj.start():jurisdiction_obj.end()]
    return render(request, 'jurisdiction_list.html', {"jurisdiction_obj": jurisdiction_page_obj,
                                                     'pages': jurisdiction_obj.pager(),
                                                     'jurisdiction_page_size': jurisdiction_page_size,
                                                     'jurisdiction_count': jurisdiction_count
                                                     })


def jurisdiction_add(request):
    '''
    增加权限url
    :param request:
    :return:
    '''
    if request.method == 'GET':
        jurisdiction_form = jurisdiction_forms.JurisdictionAddForm()
        return render(request, 'jurisdiction_add.html', {'jurisdiction_form': jurisdiction_form})
    jurisdiction_form = jurisdiction_forms.JurisdictionAddForm(request.POST)
    if jurisdiction_form.is_valid():
        try:
            jurisdiction_form.save()
        except UnicodeError as e:
            logger.error(e)
            return render(request, '500.html')
        return redirect('jurisdiction_list')
    return render(request, 'jurisdiction_add.html', {'jurisdiction_form': jurisdiction_form})


def jurisdiction_edit(request, jid):
    '''
    编辑权限url
    :param request:
    :param jid:
    :return:
    '''
    jurisdiction_form = models.Jurisdiction.objects.filter(id=jid).first()
    if request.method == 'GET':
        jurisdictions_form = jurisdiction_forms.JurisdictionEditForm(instance=jurisdiction_form)
        return render(request, 'jurisdiction_add.html', {'jurisdiction_form': jurisdictions_form, "jid": jid})
    jurisdictions_form = jurisdiction_forms.JurisdictionEditForm(data=request.POST, instance=jurisdiction_form)
    if jurisdictions_form.is_valid():
        try:
            jurisdictions_form.save()
        except UnicodeError as e:
            logger.error(e)
            return render(request, '500.html')
        return redirect('jurisdiction_list')
    return render(request, 'jurisdiction_add.html', {'jurisdiction_form': jurisdictions_form, "jid": jid})


def jurisdiction_delete(request, jid):
    '''
    删除权限url
    :param request:
    :param jid:
    :return:
    '''
    models.Jurisdiction.objects.filter(id=jid).delete()
    return redirect('jurisdiction_list')
